﻿using Caliburn.Micro;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace KouDai.Phone.ViewModels
{
    public class HotDetailPageViewModel : Conductor<IScreen>.Collection.OneActive
    {
        #region Fileds
        private readonly Func<ImageWallViewModel> _createTab;
        private readonly Func<RankingListViewModel> _createRankTab;
        private INavigationService _navigationService;
        #endregion

        #region Constructor
        public HotDetailPageViewModel(Func<ImageWallViewModel> createTab,Func<RankingListViewModel> createRankTab,INavigationService navigationService)
        {
            _createTab = createTab;
            _createRankTab = createRankTab;
            _navigationService = navigationService;
        }
        #endregion

        #region Properties

        public string SubHotInfosStr { get; set; }
        public string CombineAppId { get; set; }
        #endregion

        #region override
        protected override void OnInitialize()
        {

            if (SubHotInfosStr == null)
            {
                throw new ArgumentNullException("SubHotInfosStr");
            }
            Dictionary<string, string> subHotInfos = JsonConvert.DeserializeObject<Dictionary<string, string>>(SubHotInfosStr);
            int numberOfTabs = subHotInfos.Count;
            if (numberOfTabs > 0)
            {
                Enumerable.Range(1, numberOfTabs).Apply(x =>
                {
                    if (this.DisplayName.Equals("排行榜"))
                    {
                        var tab = _createRankTab();
                        string combineAppId = subHotInfos.Keys.ElementAt(x - 1);
                        string combineAppName = subHotInfos.Values.ElementAt(x - 1);
                        tab.DisplayName = combineAppName;
                        tab.CombineAppId = combineAppId;
                        Items.Add(tab);
                    }
                    else
                    {
                        var tab = _createTab();
                        string combineAppId = subHotInfos.Keys.ElementAt(x - 1);
                        string combineAppName = subHotInfos.Values.ElementAt(x - 1);
                        tab.DisplayName = combineAppName;
                        tab.CombineAppId = combineAppId;
                        Items.Add(tab);
                    }
                });
            }
            else if (numberOfTabs == 0)
            {
                var tab = _createTab();
                    tab.DisplayName = this.DisplayName;
                    tab.CombineAppId = CombineAppId;
                    Items.Add(tab);
            }
            ActivateItem(Items[0]);
        }
        #endregion

        #region Method

        public void Refersh()
        {
            ImageWallViewModel activeItem = this.ActiveItem as ImageWallViewModel;
            activeItem.PullDownToFresh();
        }

        public void Search()
        {
            //_navigationService.UriFor<SearchViewModel
        }

        #endregion

        #region ViewMethod
        
        #endregion
    }
}
